﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Hr_DepartmentInfo。
    /// </summary>
    public class Hr_DepartmentInfo
    {
        public Hr_DepartmentInfo()
        { }
        #region  成员方法


        public static string GetIdByDepName(string depname)
        {            
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select DepartId from Hr_DepartmentInfo where DepartName='{0}'",depname);
            Object ob= db.ExecuteScalar(CommandType.Text,strSql.ToString());
            if (ob != null)
                return ob.ToString();
            else
                return "";
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid DepartId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Hr_DepartmentInfo where DepartId=@DepartId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, DepartId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_DepartmentInfo(");
            strSql.Append("DepartId,DepartCode,DepartName,CityId,City,CompanyId,CompanyName,DepartKind,DepartProperty,Tel,IsOpen,IsCalculate,Email,Address,Region,RegionCtr,IsContractCenter,AddTime,AddPerson,DepartSrc,IsRoof)");

            strSql.Append(" values (");
            strSql.Append("@DepartId,@DepartCode,@DepartName,@CityId,@City,@CompanyId,@CompanyName,@DepartKind,@DepartProperty,@Tel,@IsOpen,@IsCalculate,@Email,@Address,@Region,@RegionCtr,@IsContractCenter,@AddTime,@AddPerson,@DepartSrc,@IsRoof)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartCode", DbType.AnsiString, model.DepartCode);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "City", DbType.AnsiString, model.City);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "DepartKind", DbType.AnsiString, model.DepartKind);
            db.AddInParameter(dbCommand, "DepartProperty", DbType.Int32, model.DepartProperty);
            db.AddInParameter(dbCommand, "Tel", DbType.AnsiString, model.Tel);
            db.AddInParameter(dbCommand, "IsOpen", DbType.Boolean, model.IsOpen);
            db.AddInParameter(dbCommand, "IsCalculate", DbType.Boolean, model.IsCalculate);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Region", DbType.AnsiString, model.Region);
            db.AddInParameter(dbCommand, "RegionCtr", DbType.AnsiString, model.RegionCtr);
            db.AddInParameter(dbCommand, "IsContractCenter", DbType.Boolean, model.IsContractCenter);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "DepartSrc", DbType.AnsiString, model.DepartSrc);
            db.AddInParameter(dbCommand, "IsRoof", DbType.Boolean, model.IsRoof);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_DepartmentInfo set ");
            strSql.Append("DepartCode=@DepartCode,");
            strSql.Append("DepartName=@DepartName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("City=@City,");            
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("DepartKind=@DepartKind,");
            strSql.Append("DepartProperty=@DepartProperty,");
            strSql.Append("Tel=@Tel,");
            strSql.Append("IsOpen=@IsOpen,");
            strSql.Append("IsCalculate=@IsCalculate,");
            strSql.Append("Email=@Email,");
            strSql.Append("Address=@Address,");
            strSql.Append("Region=@Region,");
            strSql.Append("RegionCtr=@RegionCtr,");
            strSql.Append("IsContractCenter=@IsContractCenter,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("IsRoof=@IsRoof,");
            strSql.Append("DepartSrc=@DepartSrc");
            strSql.Append(" where DepartId=@DepartId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartCode", DbType.AnsiString, model.DepartCode);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "City", DbType.AnsiString, model.City);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "DepartKind", DbType.AnsiString, model.DepartKind);
            db.AddInParameter(dbCommand, "DepartProperty", DbType.Int32, model.DepartProperty);
            db.AddInParameter(dbCommand, "Tel", DbType.AnsiString, model.Tel);
            db.AddInParameter(dbCommand, "IsOpen", DbType.Boolean, model.IsOpen);
            db.AddInParameter(dbCommand, "IsCalculate", DbType.Boolean, model.IsCalculate);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Region", DbType.AnsiString, model.Region);
            db.AddInParameter(dbCommand, "RegionCtr", DbType.AnsiString, model.RegionCtr);
            db.AddInParameter(dbCommand, "IsContractCenter", DbType.Boolean, model.IsContractCenter);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "DepartSrc", DbType.AnsiString, model.DepartSrc);
            db.AddInParameter(dbCommand, "IsRoof", DbType.Boolean, model.IsRoof);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid DepartId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_DepartmentInfo ");
            strSql.Append(" where DepartId=@DepartId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, DepartId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo GetModel(Guid DepartId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartId,DepartCode,DepartName,CityId,City,CompanyId,CompanyName,DepartKind,DepartProperty,Tel,IsOpen,IsCalculate,Email,Address,Region,RegionCtr,IsContractCenter,AddTime,AddPerson,DepartSrc,IsRoof from Hr_DepartmentInfo ");
            strSql.Append(" where DepartId=@DepartId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, DepartId);
            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static string GetComCode(Guid CompanyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartCode from Hr_DepartmentInfo ");
            strSql.Append(" where DepartProperty=200 and CompanyId=@CompanyId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, CompanyId);
            string retCode = string.Empty;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    retCode = CodeBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return retCode;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartId,DepartCode,DepartName,CityId,City,CompanyId,CompanyName,DepartKind,DepartProperty,Tel,IsOpen,IsCalculate,Email,Address,Region,RegionCtr,IsContractCenter,AddTime,AddPerson,DepartSrc,IsRoof ");
            strSql.Append(" FROM Hr_DepartmentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取部门树节点
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public static DataSet GetTreeList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select b.DepartId as val,b.DepartName as txt,b.DepartProperty as type,b.DepartCode as code,b.cityid,b.city,b.companyid,b.companyname as company from Hr_Struct_DepartTreeRelation left join Hr_DepartmentInfo as b on Hr_Struct_DepartTreeRelation.ChildId=b.DepartId ");
            
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + " and b.isopen=1 order by b.departname");
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取人的树节点
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public static DataSet GetUserTreeList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId as val,UserName as txt,700 as type,SerialCode as code,cityid,cityname as city,companyid,companyname as company from Hr_UserInfo");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + "  order by usercode");
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Hr_DepartmentInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartId,DepartCode,DepartName,CityId,City,CompanyId,CompanyName,DepartKind,DepartProperty,Tel,IsOpen,IsCalculate,Email,Address,Region,RegionCtr,IsContractCenter,AddTime,AddPerson,DepartSrc,IsRoof ");
            strSql.Append(" FROM Hr_DepartmentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> list = new List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 直接执行SQL得到部门数据
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetListBySql(string strSql)
        {
            List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> list = new List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        public static List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> GetListArray(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartId,DepartCode,DepartName,CityId,City,CompanyId,CompanyName,DepartKind,DepartProperty,Tel,IsOpen,IsCalculate,Email,Address,Region,RegionCtr,IsContractCenter,AddTime,AddPerson,DepartSrc,IsRoof ");
            strSql.Append(" FROM Hr_DepartmentInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            if (strOrder.Trim() != "")
            {
                strSql.Append(" Order By " + strOrder);
            }

            List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> list = new List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo model = new Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo();
            object ojb;
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.DepartCode = dataReader["DepartCode"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["DepartName"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartName = ojb.ToString();
            }
            model.City = dataReader["City"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["DepartKind"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartKind = ojb.ToString();
            }
            ojb = dataReader["DepartProperty"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartProperty = (int)ojb;
            }
            model.Tel = dataReader["Tel"].ToString();
            ojb = dataReader["IsOpen"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsOpen = (bool)ojb;
            }
            ojb = dataReader["IsCalculate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsCalculate = (bool)ojb;
            }
            model.Email = dataReader["Email"].ToString();
            model.Address = dataReader["Address"].ToString();
            model.Region = dataReader["Region"].ToString();
            model.RegionCtr = dataReader["RegionCtr"].ToString();
            ojb = dataReader["IsContractCenter"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsContractCenter = (bool)ojb;
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            model.DepartSrc = dataReader["DepartSrc"].ToString();
            ojb = dataReader["IsRoof"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsRoof = (bool)ojb;
            }
            return model;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static string CodeBind(IDataReader dataReader)
        {
            string strCode = string.Empty;
            strCode = dataReader["DepartCode"].ToString();
            return strCode;
        }

        #endregion  成员方法
    }
}

